package br.com.JZSolution.domineduplex.gui.dialogos;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.com.JZSolution.domineduplex.DomineDuplex;
import br.com.JZSolution.domineduplex.controle.ControladorDeUsuario;

public class AdicionarContatoDialogo extends JDialog {
		
	JTextField nome;
	
	public AdicionarContatoDialogo(){
		JPanel panelGeral = new JPanel(new GridLayout(2,0));
		
		JLabel label = new JLabel("Email do usuário:");
		nome = new JTextField(20);
		JButton add = new JButton("Adicionar");
		add.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				adicionarContato(nome.getText());
				dispose();
			}
		});
		JButton close = new JButton("Fechar");
		close.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		JPanel painelNome = new JPanel(new FlowLayout(FlowLayout.LEFT));
		painelNome.add(label);
		painelNome.add(nome);
		JPanel painelBtn = new JPanel(new FlowLayout(FlowLayout.CENTER));
		painelBtn.add(add);
		painelBtn.add(close);
		
		panelGeral.add(painelNome,0);
		panelGeral.add(painelBtn,1);
		
		this.add(panelGeral);
		this.setMinimumSize(new Dimension(400,150));
		this.setLocationRelativeTo(DomineDuplex.getInstance().getMainframe());
		this.setTitle("Adicionar contato");
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	private void adicionarContato(String nome){
		ControladorDeUsuario.getInstance().adicionarContato(nome);
	}
}
